﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;

namespace DealKhuyenMaiV2.com.Controllers
{
    [DealKhuyenMaiV2.com.Controllers.AccountController.AdminAuthorize]
    public class CatalogyManagerController : BaseController
    {
        //
        // GET: /CatologyManager/

        public ViewResult Index()
        {
            return View(DbConText.Catalogies.ToList());
        }

        //
        // GET: /CatologyManager/Details/5

        public ViewResult Details(int id)
        {
            Catalogy catalogy = DbConText.Catalogies.Single(c => c.IDCatalogy == id);
            return View(catalogy);
        }

        //
        // GET: /CatologyManager/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /CatologyManager/Create

        [HttpPost]
        public ActionResult Create(Catalogy catalogy)
        {
            catalogy.DateAdded = DateTime.Now;
            catalogy.DateModified = DateTime.Now;
            if (ModelState.IsValid)
            {
                DbConText.Catalogies.AddObject(catalogy);
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                //for debug only
                var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();
            }

            return View(catalogy);
        }

        //
        // GET: /CatologyManager/Edit/5

        public ActionResult Edit(int id)
        {
            Catalogy catalogy = DbConText.Catalogies.Single(c => c.IDCatalogy == id);
            return View(catalogy);
        }

        //
        // POST: /CatologyManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Catalogy catalogy)
        {
            Catalogy _catalogy = DbConText.Catalogies.Single(c => c.IDCatalogy == id);
            {
                _catalogy.NameCatalogy = catalogy.NameCatalogy;
                _catalogy.Image = catalogy.Image;
                _catalogy.IDParent = catalogy.IDParent;
                //_catalogy.DateAdded = catalogy.DateAdded;
                _catalogy.DateModified = DateTime.Now;
                _catalogy.Status = catalogy.Status;
                _catalogy.SortOrder = catalogy.SortOrder;
                _catalogy.Description = catalogy.Description;
            }
            if (ModelState.IsValid)
            {
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(catalogy);
        }

        //
        // GET: /CatologyManager/Delete/5
        public ActionResult Delete(int id)
        {
            //Catalogy catalogy = DbConText.Catalogies.Single(c => c.IDCatalogy == id);
            //return View(catalogy);
            Catalogy catalogy = DbConText.Catalogies.Single(c => c.IDCatalogy == id);
            DbConText.Catalogies.DeleteObject(catalogy);
            DbConText.SaveChanges();
            return RedirectToAction("Index");
        }

       
        //
        // POST: /CatologyManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Catalogy catalogy = DbConText.Catalogies.Single(c => c.IDCatalogy == id);
            DbConText.Catalogies.DeleteObject(catalogy);
            DbConText.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}